package cn.gson.springboot.controller.privilegecontroller;
import cn.gson.springboot.model.pjos.Emp;
import cn.gson.springboot.model.pjos.Menu;
import cn.gson.springboot.model.pjos.Permissionsset;
import cn.gson.springboot.model.service.privilegeservice.UserLoginService;
import cn.gson.springboot.model.vo.privilegevo.EmpVo;
import cn.gson.springboot.model.vo.privilegevo.Router;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/user")
@SessionAttributes({"user"})
public class UserLoginController {

    @Autowired
    UserLoginService userLoginService;

    //登录
    @PostMapping("/login")
    @ResponseBody
    public String login(@RequestBody Emp emp, HttpServletRequest request){

        Emp emp1 = userLoginService.login(emp);//查询用户
        if(emp1 != null){//用户存在 判断是否离职
            if(emp1.getEmpIsNot().equals("1")){//在职
//                session.removeAttribute("user");
//                request.getSession().removeAttribute("user");
                request.getSession().setAttribute("user",emp1);//将用户信息保存起来
                request.getSession().setMaxInactiveInterval(3*60*60);//设置session有效时长
                System.err.println("用户："+request.getSession().getAttribute("user"));
                return "success";
            }else{//离职
                return "out";
            }
        }else{
            return "error";//不存在用户
        }

    }
    @RequestMapping("/emp_info")
    @ResponseBody
    public Map<String,Object> empInfo(@RequestBody Emp emp){
        Map<String,Object> map = new HashMap<>();
        Emp empinfo=userLoginService.login(emp);//用户信息
        List<Permissionsset> permissionssetList = userLoginService.user_meun(empinfo.getEmployeesId());//用户权限
        List<Menu> perssionButton = userLoginService.perssionButton(empinfo.getEmployeesId());//按钮
        map.put("empInfo",empinfo);
        map.put("permissionssetList",permissionssetList);
        map.put("permissionButton",perssionButton);
        return  map;
    }

    //首页菜单栏
    @RequestMapping("/meun")
    @ResponseBody
    //左边菜单栏
    public List<Permissionsset> user_meun(HttpSession session){
        Emp emp = (Emp)session.getAttribute("user");
        return userLoginService.user_meun(emp.getEmployeesId());
    }

    //退出登录
    @RequestMapping("/login_out")
    @ResponseBody
    public void loginOut(HttpSession session){
        session.removeAttribute("user");//清除session
    }

   //按钮权限
    @RequestMapping("/perssion_button")
    @ResponseBody
    public List<Menu> perssionButton(HttpSession session){
        Emp emp = (Emp)session.getAttribute("user");
        return userLoginService.perssionButton(emp.getEmployeesId());
    }
    @RequestMapping("/router")
    @ResponseBody
    public List<Router> routerList(Integer employeesId){
        return  userLoginService.routerList(employeesId);
    }
    /**
     * 数据控制
     */

    /**
     * 用于个人中心
     * @param employeesId
     * @return
     */
    @RequestMapping("/personal")
    @ResponseBody
    public Emp personal(Integer employeesId){
        return userLoginService.personal(employeesId);
    }

    /**
     * 修改部分信息
     */
    @RequestMapping("/update_some_info")
    @ResponseBody
    public void updateSomeInfo(@RequestBody EmpVo emp){
        userLoginService.updateSomeInfo(emp);
    }

    /**
     * 旧密码是否正确
     */
    @RequestMapping("/yzPwd")
    @ResponseBody
    public int yzPwd(Integer employeesId,String oldPwd){
       return userLoginService.yzPwd(employeesId,oldPwd);
    }

    /**
     *  修改密码
     */
    @RequestMapping("/updatePwd")
    @ResponseBody
    public void updatePwd(Integer employeesId,String newPwd){
         userLoginService.updatePwd(employeesId,newPwd);
    }

    /**
     * 重置密码
     */
    @RequestMapping("/restPwd")
    @ResponseBody
    public void restPwd(Integer employeesId){
        userLoginService.restPwd(employeesId);
    }

}
